package com.ruoyi.project.im.groups.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.im.doyuming.domain.Doyuming;
import com.ruoyi.project.im.doyuming.service.IDoyumingService;
import com.ruoyi.project.im.members.domain.GroupMembers;
import com.ruoyi.project.im.personhost.domain.Personhost;
import com.ruoyi.project.im.personhost.service.IPersonhostService;
import com.ruoyi.project.im.utils.Http;
import com.ruoyi.project.im.utils.RandomNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.im.groups.mapper.GroupsMapper;
import com.ruoyi.project.im.groups.domain.Groups;
import com.ruoyi.project.im.groups.service.IGroupsService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 群信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-10
 */
@Service
public class GroupsServiceImpl implements IGroupsService
{
    @Autowired
    private GroupsMapper groupsMapper;

    @Autowired
    private IGroupsService  groupsService;

    @Autowired
    private IDoyumingService iDoyumingService;

    @Autowired
    private IPersonhostService iPersonhostService;

    /**
     * 查询群信息
     *
     * @param groupId 群信息主键
     * @return 群信息
     */
    @Override
    public Groups selectGroupsByGroupId(String groupId)
    {
        return groupsMapper.selectGroupsByGroupId(groupId);
    }

    @Override
    public Groups selectGroupsByUserId(String userId){
        return groupsMapper.selectGroupsByUserId(userId);
    }

    /**
     * 查询群信息列表
     *
     * @param groups 群信息
     * @return 群信息
     */
    @Override
    public List<Groups> selectGroupsList(Groups groups)
    {
        return groupsMapper.selectGroupsList(groups);
    }

    /**
     * 新增群信息
     *
     * @param groups 群信息
     * @return 结果
     */
    @Override
    public int insertGroups(Groups groups)
    {


        String groupID=RandomNum.getOperationID();
        Map<String,Object> map = new HashMap<>();
        map.put("ex","");
        map.put("faceURL","");
        map.put("groupID",groupID);
        map.put("groupName",groups.getName());
        map.put("groupType",0);
        map.put("introduction",groups.getIntroduction());
        map.put("notification",groups.getNotification());
        map.put("operationID",RandomNum.getOperationID());
        map.put("ownerUserID",groups.getCreatorUserId());
        List list=new ArrayList();
        Map<String,Object> member = new HashMap<>();
        member.put("roleLevel",1);
        member.put("userID",groups.getCreatorUserId());
        list.add(member);
        map.put("memberList",list);
        map.put("secret",Http.SECRET);
        String jsonString = JSON.toJSONString(map);
        JSONObject result = Http.post(Http.ADD_GROUP,jsonString);
        if (result.getIntValue("errCode") == 0){
            Doyuming doyuming = iDoyumingService.selectDoyumingBYOne("1");
            Personhost personhost=new Personhost();
            personhost.setUserId(groups.getCreatorUserId());
            personhost.setGroupId(groupID);
            personhost.setUrl("http://"+ RandomNum.randomxiaoxie(10)+"."+doyuming.getDomain());
            personhost.setShortcode(RandomNum.randomxiaoxie(4));
            personhost.setLeixing(1L);
            personhost.setStatus(2L);
            int i=iPersonhostService.insertPersonhost(personhost);
            if(i>0){
                doyuming.setFenpeicount(doyuming.getFenpeicount()+1);
                iDoyumingService.updateDoyuming(doyuming);
            }
        }
        groups.setGroupId(groupID);
        groups.setNotificationUserId(groups.getCreatorUserId());
        return groupsService.updateGroups(groups);


//        groups.setCreateTime(DateUtils.getNowDate());
//        return groupsMapper.insertGroups(groups);
    }

    /**
     * 修改群信息
     *
     * @param groups 群信息
     * @return 结果
     */
    @Override
    public int updateGroups(Groups groups)
    {
        return groupsMapper.updateGroups(groups);
    }

    /**
     * 批量删除群信息
     *
     * @param groupIds 需要删除的群信息主键
     * @return 结果
     */
    @Override
    public int deleteGroupsByGroupIds(String groupIds)
    {
        return groupsMapper.deleteGroupsByGroupIds(Convert.toStrArray(groupIds));
    }

    /**
     * 删除群信息信息
     *
     * @param groupId 群信息主键
     * @return 结果
     */
    @Override
    public int deleteGroupsByGroupId(String groupId)
    {
        return groupsMapper.deleteGroupsByGroupId(groupId);
    }

    /**
     * 查找当前用户是否在群聊中
     *
     * @param groupId 群id
     * @param userId 用户id
     * @return 结果
     */
    public int selectGroupsByGroupIdAndUserId(String groupId, String userId){
        return groupsMapper.selectGroupsByGroupIdAndUserId(groupId,userId);
    }

}
